package com.javasm.controller;


import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCode;
import com.javasm.entity.ReturnResult;
import com.javasm.service.Impl.LoginServiceImpl;
import com.javasm.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/login")

public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* 允许跨域访问相关参数设置 */
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
        /* 允许跨域的请求⽅法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");

        /* 允许跨域的主机地址 */
//        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
//        /* 允许跨域的请求⽅法GET, POST, HEAD 等 */
//        resp.setHeader("Access-Control-Allow-Methods", "*");
//        /* 重新预检验跨域的缓存时间 (s) */
//        resp.setHeader("Access-Control-Max-Age", "3600");
//        /* 允许跨域的请求头 */
//        resp.setHeader("Access-Control-Allow-Headers", "*");
//        /* 是否携带cookie */
//        resp.setHeader("Access-Control-Allow-Credentials", "true");
//
//        1.接受请求参数，转化格式 封装对象
            req.setCharacterEncoding("Utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
////        2.调用service
//        LoginServiceImpl adminUserService = new LoginServiceImpl();
//        AdminUser userByUsername = adminUserService.getuserByusername(username);
        //2.调用service
        //2.调用service
        LoginService loginService = new LoginServiceImpl();
        AdminUser userByUsername = loginService.getuserByusername(username);

//        3.根据执行结果返回结果，显示页面
        ReturnResult rrs = new ReturnResult();
        if(userByUsername!=null){
            //匹配其他信息

            if(userByUsername.getIsvalid().equals(2)){
                //用户休假状态
                rrs.setReturnCode(ReturnCode.LOGIN_FAILED_REST.getCode());
                rrs.setReturnMsg(ReturnCode.LOGIN_FAILED_REST.getMsg());
            } else if(userByUsername.getIsvalid().equals(0)){
                //用户离职状态
                rrs.setReturnCode(ReturnCode.LOGIN_FAILED_JUMP.getCode());
                rrs.setReturnMsg(ReturnCode.LOGIN_FAILED_JUMP.getMsg());
            }else{
                //匹配密码
                if(userByUsername.getPassword().equals(password)){
                    //登录成功
                    //登录成功的用户信息存入session 方便后续服务使用
                    HttpSession session = req.getSession();
                    session.setAttribute("loginUser",userByUsername);
                    //更新最后登录时间
                    loginService.updateUserLoginTime(userByUsername.getUid());
                    //查询当前用户的动态菜单数据
                    List<AdminMenu> userMenus = loginService.ListMenuWithSubMenuByUid(userByUsername.getUid());
                    session.setAttribute("userMenus",userMenus);

//                    List<AdminMenu> userMenus = loginService.getuserByusername(userByUsername.getUid());
//                    session.setAttribute("userMenus",userMenus);

                    rrs.setReturnCode(ReturnCode.LOGIN_SUCCESS.getCode());
                    rrs.setReturnMsg(ReturnCode.LOGIN_SUCCESS.getMsg());
                    rrs.setReturnData(userByUsername);

                }else{
                    //密码不对
                    rrs.setReturnCode(ReturnCode.LOGIN_FAILED_NAME_PWD.getCode());
                    rrs.setReturnMsg(ReturnCode.LOGIN_FAILED_NAME_PWD.getMsg());
                }

            }

        }else{
            //用户名不对
            rrs.setReturnCode(ReturnCode.LOGIN_FAILED_NAME_PWD.getCode());
            rrs.setReturnMsg(ReturnCode.LOGIN_FAILED_NAME_PWD.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }
}